package cn.cai.controller;


import cn.cai.commonUtils.Result;
import cn.cai.exceptionHandler.GuliException;
import cn.cai.pojo.EduCourse;
import cn.cai.pojo.vo.CourseListVo;
import cn.cai.pojo.vo.CoursePublicVo;
import cn.cai.service.IEduCourseService;
import cn.cai.pojo.vo.CourseInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author caishenao
 * @since 2021-08-17
 */
@RestController
@RequestMapping("/eduservice/course")
@Api(value="课程操作",tags="课程操作接口")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private IEduCourseService courseService;


    //添加课程基本信息的方法
    @PostMapping("/addCourseInfo")
    @ApiOperation("添加课程基本信息")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String courseId = courseService.saveCourseInfo(courseInfoVo);
        return Result.success().data("courseId",courseId);
    }

    //根据courseId查询课程基本信息
    @GetMapping("/findCourseInfoById/{courseId}")
    @ApiOperation("根据课程id查询课程的基本信息")
    public Result findCourseInfoById(@PathVariable("courseId") String courseId){
        CourseInfoVo courseInfoVo = courseService.findCourseInfoById(courseId);
        return Result.success().data("courseInfoVo",courseInfoVo);
    }

    //根据传入的courseInfoVo，对edu_course和edu_course_description两张表进行修改
    @PostMapping("/updateCourseInfo")
    @ApiOperation("修改课程信息")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //进行简单的判断
        if (courseInfoVo == null){
            throw new GuliException(20001,"请正确使用接口");
        }
        courseService.updateCourseInfo(courseInfoVo);
        return Result.success().data("msg","修改成功");
    }

    //根据课程id查询课程信息
    @GetMapping("/getPublishCourseInfo/{courseId}")
    @ApiOperation("根据课程id查询课程信息")
    public Result getPublishCourseInfo(@PathVariable("courseId")
                                                   @ApiParam(name = "courseId",value = "课程id",required = true)
                                                   String courseId){
        CoursePublicVo coursePublicVo = courseService.getPublishCourseInfo(courseId);
        return Result.success().data("coursePublicVo",coursePublicVo);
    }

    //课程的最终发布
    @PutMapping("/postACourse/{courseId}")
    @ApiOperation("课程的最终的发布")
    public Result postACourse(@PathVariable("courseId")
                                          @ApiParam(name = "curseId",value = "要发布课程的id",required = true)
                                          String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        boolean b = courseService.updateById(eduCourse);

        return b?Result.success().data("msg","课程发布成功"):Result.error().data("msg","课程发布失败");
    }

    //--TODO 实现课程查询带分页
    @ApiOperation("课程分页")
    @GetMapping("/getCourseList/{pageNum}/{pageSize}")
    public Result getCourseList(@PathVariable("pageNum")
                                @ApiParam(name = "pageNum",value = "当前页")
                                            Integer pageNum,
                                @PathVariable("pageSize")
                                @ApiParam(name = "pageSize",value = "页面大小")
                                        Integer pageSize){
        if (Objects.isNull(pageNum)){
            //设置默认页数
            pageNum = 1;
        }
        if (Objects.isNull(pageSize)){
            //设置默认页数大小
            pageSize = 10;
        }
        //构建page实例
        Page<EduCourse> pageQuery = new Page<>(pageNum,pageSize);
        Page<EduCourse> page = courseService.page(pageQuery);
        return Result.success().data("list",page);
    }

    //实现课程信息多条件查询
    @ApiOperation("课程信息多条件查询")
    @PostMapping("/multipleConditionsQuery/{pageNum}/{pageSize}")
    public Result multipleConditionsQuery(@PathVariable("pageNum")
                                                  @ApiParam(name = "pageNum",value = "当前页数")
                                                      Integer pageNum,
                                          @PathVariable("pageSize")
                                                  @ApiParam(name = "pageSize",value = "页的大小")
                                                      Integer pageSize,
                                          @RequestBody
                                                  @ApiParam(name = "courseListVo",value = "提交的查询对象")
                                                      CourseListVo courseListVo){
        //为路径设置初始值，入没有传
        if (Objects.isNull(pageNum)){
            pageNum = 1;
        }
        if (Objects.isNull(pageSize)){
            pageSize = 10;
        }
        //创建Page对象
        Page<EduCourse> page = new Page<>(pageNum,pageSize);
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(courseListVo)){
            if (Objects.nonNull(courseListVo.getTitle())){
                queryWrapper.like("title",courseListVo.getTitle());
            }
            if (Objects.nonNull(courseListVo.getStatus())){
                queryWrapper.eq("status",courseListVo.getStatus());
            }
            if (Objects.nonNull(courseListVo.getSubjectId())){
                //添加二级课程筛选
                queryWrapper.eq("subject_id",courseListVo.getSubjectId());
            }
            if (Objects.nonNull(courseListVo.getTeacherId())){
                //添加讲师筛选
                queryWrapper.eq("teacher_id",courseListVo.getTeacherId());
            }
            if (Objects.nonNull(courseListVo.getSubjectParentId())){
                //添加一级分类筛选
                queryWrapper.eq("subject_parent_id",courseListVo.getSubjectParentId());
            }
        }
        //按照修改时间的倒叙查询
        queryWrapper.orderByDesc("gmt_modified");
        Page<EduCourse> list = courseService.page(page, queryWrapper);
        if (Objects.isNull(list)){
            return Result.error().data("msg","没有符合课程！");
        }
        return Result.success().data("list",list);
    }
}
